<template>
  <div class="google-auth-test">
    <div class="container">
      <h1>Google认证测试页面</h1>
      
      <!-- 配置状态检查 -->
      <div class="config-status">
        <h2>配置状态</h2>
        <div class="status-item">
          <span class="label">Google Client ID:</span>
          <span :class="['status', clientIdStatus.valid ? 'valid' : 'invalid']">
            {{ clientIdStatus.message }}
          </span>
        </div>
        <div class="status-item">
          <span class="label">Google Identity Services:</span>
          <span :class="['status', googleServicesLoaded ? 'valid' : 'invalid']">
            {{ googleServicesLoaded ? '已加载' : '未加载' }}
          </span>
        </div>
        <div class="status-item">
          <span class="label">重定向URI:</span>
          <span class="status">{{ redirectUri }}</span>
        </div>
      </div>

      <!-- 测试按钮 -->
      <div class="test-buttons">
        <h2>测试Google认证</h2>
        
        <!-- 标准Google登录按钮 -->
        <div class="test-section">
          <h3>标准Google登录按钮</h3>
          <GoogleLoginButton
            @success="handleSuccess"
            @error="handleError"
          />
        </div>

        <!-- Google One Tap -->
        <div class="test-section">
          <h3>Google One Tap</h3>
          <GoogleLoginButton
            :use-one-tap="true"
            @success="handleSuccess"
            @error="handleError"
          />
        </div>

        <!-- 手动弹窗测试 -->
        <div class="test-section">
          <h3>手动弹窗测试</h3>
          <button @click="testPopup" :disabled="isLoading" class="test-btn">
            {{ isLoading ? '测试中...' : '测试弹窗认证' }}
          </button>
        </div>
      </div>

      <!-- 测试结果 -->
      <div v-if="testResult" class="test-result">
        <h2>测试结果</h2>
        <div class="result-content">
          <pre>{{ JSON.stringify(testResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- 错误信息 -->
      <div v-if="error" class="error-section">
        <h2>错误信息</h2>
        <div class="error-content">
          {{ error }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { GoogleAuthUtils } from '../utils/googleAuth'
import GoogleLoginButton from '../components/GoogleLoginButton.vue'
import { useToast } from 'vue-toastification'

const toast = useToast()

// 响应式数据
const clientIdStatus = ref({ valid: false, message: '检查中...' })
const googleServicesLoaded = ref(false)
const redirectUri = ref('')
const testResult = ref(null)
const error = ref('')
const isLoading = ref(false)

// 检查配置状态
function checkConfiguration() {
  // 检查Client ID
  const clientId = import.meta.env.VITE_GOOGLE_CLIENT_ID
  if (!clientId || clientId === 'your-google-client-id.apps.googleusercontent.com') {
    clientIdStatus.value = { valid: false, message: '未配置或使用默认占位符' }
  } else {
    clientIdStatus.value = { valid: true, message: `已配置: ${clientId.substring(0, 20)}...` }
  }

  // 检查重定向URI
  redirectUri.value = import.meta.env.VITE_GOOGLE_REDIRECT_URI || `${window.location.origin}/auth/google/callback`

  // 检查Google Identity Services
  checkGoogleServices()
}

// 检查Google Identity Services是否加载
function checkGoogleServices() {
  const checkInterval = setInterval(() => {
    if (window.google && window.google.accounts) {
      googleServicesLoaded.value = true
      clearInterval(checkInterval)
    }
  }, 100)

  // 10秒后停止检查
  setTimeout(() => {
    clearInterval(checkInterval)
    if (!googleServicesLoaded.value) {
      error.value = 'Google Identity Services 加载超时'
    }
  }, 10000)
}

// 测试弹窗认证
async function testPopup() {
  if (!GoogleAuthUtils.isConfigured()) {
    error.value = 'Google OAuth 未配置'
    return
  }

  isLoading.value = true
  error.value = ''

  try {
    const code = await GoogleAuthUtils.openAuthPopup()
    testResult.value = {
      type: 'popup',
      code: code,
      timestamp: new Date().toISOString()
    }
    toast.success('弹窗认证测试成功')
  } catch (err) {
    error.value = err instanceof Error ? err.message : '弹窗认证测试失败'
    toast.error(error.value)
  } finally {
    isLoading.value = false
  }
}

// 处理认证成功
function handleSuccess(result: any) {
  testResult.value = {
    type: 'success',
    data: result,
    timestamp: new Date().toISOString()
  }
  error.value = ''
  toast.success('Google认证成功')
}

// 处理认证错误
function handleError(errorMsg: string) {
  error.value = errorMsg
  testResult.value = null
  toast.error(`Google认证失败: ${errorMsg}`)
}

// 组件挂载时检查配置
onMounted(() => {
  checkConfiguration()
})
</script>

<style scoped>
.google-auth-test {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 2rem;
}

.container {
  max-width: 800px;
  margin: 0 auto;
  background: white;
  border-radius: 12px;
  padding: 2rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
  color: #333;
  margin-bottom: 2rem;
}

h2 {
  color: #555;
  border-bottom: 2px solid #eee;
  padding-bottom: 0.5rem;
  margin: 2rem 0 1rem 0;
}

h3 {
  color: #666;
  margin: 1rem 0 0.5rem 0;
}

.config-status {
  margin-bottom: 2rem;
}

.status-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0;
  border-bottom: 1px solid #eee;
}

.label {
  font-weight: 600;
  color: #333;
}

.status {
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-size: 0.9rem;
}

.status.valid {
  background: #d4edda;
  color: #155724;
}

.status.invalid {
  background: #f8d7da;
  color: #721c24;
}

.test-section {
  margin: 1.5rem 0;
  padding: 1rem;
  border: 1px solid #eee;
  border-radius: 8px;
}

.test-btn {
  background: #4285f4;
  color: white;
  border: none;
  padding: 0.75rem 1.5rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.2s;
}

.test-btn:hover:not(:disabled) {
  background: #3367d6;
}

.test-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.test-result {
  margin-top: 2rem;
}

.result-content {
  background: #f8f9fa;
  border: 1px solid #dee2e6;
  border-radius: 6px;
  padding: 1rem;
  overflow-x: auto;
}

.result-content pre {
  margin: 0;
  font-family: 'Courier New', monospace;
  font-size: 0.9rem;
}

.error-section {
  margin-top: 2rem;
}

.error-content {
  background: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
  border-radius: 6px;
  padding: 1rem;
}
</style>
